<!--
 * Copyright 2013 The Chromium Authors. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<html>
  <head>
    <script type="text/javascript">
      // A guest that has an <iframe>.
      // The <iframe> initially requests geolocation. The embedder keeps the
      // geolocation permission request hanging (by calling e.preventDefault()),
      // and the guest unloads to iframe (by setting a new src). The unload
      // will trigger cancelPermissionRequest.

      // The window reference of the embedder to send post message reply.
      var embedderWindowChannel = null;

      // Url of the iframe's initial src.
      var g_testName = 'uninitialized';

      var notifyEmbedder = function(msg_array) {
        embedderWindowChannel.postMessage(JSON.stringify(msg_array), '*');
      };

      var iframeElement;
      var iframeLoadCount;
      var iframeOnLoad = function() {
        console.log('iframeOnLoad');
        ++iframeLoadCount;
        if (iframeLoadCount == 1) {
          iframeElement.src =
              'data:text/html,<html><body>' +
              'Second iframe, this clears first iframe\'s documentElement' +
              '</body></html>';
        } else if (iframeLoadCount == 2) {
          notifyEmbedder([g_testName, 'PASSED']);
        } else {
          notifyEmbedder([g_testName, 'FAILED']);
        }
      };

      var startTest = function(iframeURL) {
        iframeElement = document.createElement('iframe');
        document.querySelector('#iframe-container').appendChild(iframeElement);
        iframeLoadCount = 0;
        iframeElement.onload = iframeOnLoad;
        iframeElement.src = iframeURL;
      };

      var onPostMessageReceived = function(e) {
        var data = JSON.parse(e.data);
        if (data[0] == 'test-cancel-geolocation') {
          window.console.log('guest: test-cancel-geolocation');
          g_testName = data[1];
          var iframeURL = data[2];
          embedderWindowChannel = e.source;
          // Start the test once we have |embedderWindowChannel|.
          startTest(iframeURL);
        }
      };
      window.addEventListener('message', onPostMessageReceived, false);
    </script>
  </head>
  <body>
    <div>
      This is a guest that has an iframe. Iframe requests geolocation. While the
      permission is being decide, the iframe is unloaded.
    </div>
    <div id="iframe-container"></div>
    <script>
      console.log('Guest loaded');
    </script>
  </body>
</html>
